package com.csun.cmny.security.core.authentication;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2021-01-18 19:49
 * @Description:
 */
public class WeiXinAuthenticationToken extends AbstractAuthenticationToken {

    private final Object principal;
    private Object nickName;
    private Object avatar;
    private Object gender;
    private Object city;

    WeiXinAuthenticationToken(Object principal, Object nickName, Object avatar, Object gender, Object city) {
        super(null);
        this.principal = principal;
        this.nickName = nickName;
        this.avatar = avatar;
        this.gender = gender;
        this.city = city;
        setAuthenticated(false);
    }

    WeiXinAuthenticationToken(Object principal, Object nickName, Object avatar, Object gender, Object city,
                               Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.principal = principal;
        this.nickName = nickName;
        this.avatar = avatar;
        this.gender = gender;
        this.city = city;
        super.setAuthenticated(true);
    }

    @Override
    public Object getCredentials() {
        return this.nickName;
    }

    @Override
    public Object getPrincipal() {
        return this.principal;
    }

    public Object getAvatar() { return this.avatar; }

    public Object getCity() {
        return this.city;
    }

    public Object getGender() {
        return this.gender;
    }

    @Override
    public void setAuthenticated(boolean isAuthenticated) {
        if (isAuthenticated) {
            throw new IllegalArgumentException("Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
        }

        super.setAuthenticated(false);
    }

    @Override
    public void eraseCredentials() {
        super.eraseCredentials();
        nickName = null;
    }
}
